﻿using System.Windows;
using System.Windows.Controls;
using Launch.ViewModel;
using Microsoft.Win32;
using Model;

namespace Launch.Views.Setting;

public partial class DevUpdateView : UserControl
{
    // ViewModel
    private readonly DevUpdateVm _vm;

    // 设备升级模型
    private UpdateDevProgram _updateDevProgram;

    public DevUpdateView()
    {
        InitializeComponent();
        _vm = new DevUpdateVm();
        DataContext = _vm;

        // 初始化设备升级模型
        _updateDevProgram = new(_vm);

        // 初始化设备列表
        _vm.DeviceList = new List<string> {"Local"};
        // 选中第一个设备
        if (_vm.DeviceList.Count > 0)
        {
            _vm.SelectedDevice = _vm.DeviceList[0];
        }
    }

    /// <summary>
    /// 开始升级按钮点击事件
    /// </summary>
    private void BtnStartUpdate_Click(object sender, RoutedEventArgs e)
    {
        _updateDevProgram.StartUpdate();
    }

    /// <summary>
    /// 点击选择升级文件按钮
    /// </summary>
    private void BtnSelectUpdateFile_Click(object sender, RoutedEventArgs e)
    {
        var openFileDialog = new OpenFileDialog();
        // 设置初始目录和过滤器
        openFileDialog.InitialDirectory = _vm.UpdateFilePath;
        openFileDialog.Filter = "升级文件(*.bin)|*.bin|所有文件(*.*)|*.*";
        // 显示对话框
        if (openFileDialog.ShowDialog() == true)
        {
            // 记录文件路径到VM
            _vm.UpdateFilePath = openFileDialog.FileName;
        }
    }
}